from contextlib import asynccontextmanager
from fastapi import FastAPI
from superdialog import DialogMachine, Flow, SessionWorker, InMemorySessionStore
flow = Flow.load("kyc.json")
worker: SessionWorker
@asynccontextmanager
async def lifespan(app: FastAPI):
global worker
worker = SessionWorker(
agent_factory=lambda: DialogMachine(flow=flow, llm="openai/gpt-5.1"),
store=InMemorySessionStore(), # swap for RedisSessionStore in production (v0.3)
)
yield
app = FastAPI(lifespan=lifespan)
@app.post("/turn")
async def turn(payload: dict):
async with worker.acquire(payload["session_id"]) as h:
result = await h.turn(payload["text"])
return {"reply": result.text}